<?php

namespace App\Http\Requests;

use App\Rules\IdentityCardRule;
use App\Rules\ProrationsRule;
use Illuminate\Foundation\Http\FormRequest;
use App\Rules\MobileRule;
use Illuminate\Validation\Rule;

class AgentPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->getMethod()) {
            case 'POST':
                return $this->rulesForAdd();
            case 'PUT':
            case 'PATCH':
                return $this->rulesForPut();
            default:
                return [];
        }
    }

    /**
     * 添加用户表单验证规则
     */
    private function rulesForAdd ()
    {
        $rules = [];

        // 验证账号
        $rules['name'] = [ 'required', 'alpha_dash',

            Rule::unique('users')->where(function ($query) {
                $query->where('type', 1);
            }),

            'min:3', 'max:16'
        ];

        // 验证邮箱
        if ($this->has('email')) {
            $rules['email'] = [
                'email',
                Rule::unique('users')->where(function ($query) {
                    $query->where('type', 1);
                })
            ];
        }

        if ($this->has('mobile')) {
            $rules['mobile'] = [
                new MobileRule()
            ];
        }

        // 身份证号码
        $rules['identity_card'] = [new IdentityCardRule()];

        // 验证真实姓名
        if ($this->has('realname')) {
            $rules['realname'] = [ 'required', 'string', 'min:2', 'max:8' ];
        }

        if ($this->has('nickname')) {
            $rules['nickname'] = [ 'required', 'string', 'min:2', 'max:8' ];
        }

        if ($this->has('prorations')) {
            $rules['prorations'] = new ProrationsRule();
        }

        if ($this->has('is_effect')) {
            $rules['is_effect'] = 'boolean';
        }

        if ($this->has('sex')) {
            $rules['sex'] = 'required|in:0,1,2';
        }

        $rules['source'] = 'required:string';

        // 验证用户密码
        $rules['password'] = "required|string|max:18|min:6";
        $rules['re_password'] = 'same:password';

        return $rules;
    }

    private function rulesForPut ()
    {
        $rules = [];

        // 验证手机
        if ($this->has('mobile')) {
            $rules['mobile'] = [
                new MobileRule(),
            ];
        }

        // 验证邮箱账号
        if ($this->has('email')) {
            $rules['email'] = [
                'email',
                Rule::unique('users')->where(function ($query) {
                    $query->where('type', 1);
                })->ignore($this->id)
            ];
        }

        // 身份证号码
        if ($this->has('identity_card')) {
            $rules['identity_card'] = [ new IdentityCardRule() ];
        }

        // 验证真实姓名
        if ($this->has('realname')) {
            $rules['realname'] = [ 'required', 'string', 'min:2', 'max:8' ];
        }

        // 验证昵称
        if ($this->has('nickname')) {
            $rules['nickname'] = [ 'required', 'string', 'min:2', 'max:8' ];
        }

        // 验证账户是否有效
        if ($this->has('is_effect')) {
            $rules['is_effect'] = 'boolean';
        }

        // 验证性别
        if ($this->has('sex')) {
            $rules['sex'] = 'required|in:0,1,2';
        }

        if ($this->has('prorations')) {
            $rules['prorations'] = new ProrationsRule();
        }

        // 验证用户密码
        if ($this->filled('password')) {
            $rules['password'] = "required|string|max:18|min:6";
            $rules['re_password'] = 'same:password';
        }

        return $rules;
    }

    public function attributes()
    {
        return [
            'identity_card' => '身份证号码'
        ];
    }
}
